﻿using Infrastructure.Attribute;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.mes.md;
using ZR.Service.mes.md.IService;
using ZR.Service.MES.md.IService;

namespace ZR.Service.MES.md
{
    [AppService(ServiceType = typeof(IMdWorklineService), ServiceLifetime = LifeTime.Transient)]
    public class MdWorklineService : BaseService<MdWorkline>, IMdWorklineService
    {


        int IMdWorklineService.AddWorkline(MdWorkline workline)
        {
            return Insert(workline);
        }

        int IMdWorklineService.deleteWorkline(int[] ids)
        {
            return Delete(ids);
        }

        // 导航查询
        (int, List<MdWorkline>) IMdWorklineService.GetAll(string lineCode, string lineName, int pageNum, int pageSize)
        {
            int totalNum = 0;
            var predicate = Expressionable.Create<MdWorkline>()
                .AndIF(!string.IsNullOrEmpty(lineCode), it => it.LineCode.Contains(lineCode))
                .AndIF(!string.IsNullOrEmpty(lineName), it => it.LineName.Contains(lineName))
                .ToExpression();
            List<MdWorkline> data = Context.Queryable<MdWorkline>().Includes(x => x.Workshop).Where(predicate).ToPageList(pageNum, pageSize, ref totalNum);
            return (totalNum, data);

        }

        int IMdWorklineService.UpdateWorkline(MdWorkline workline)
        {
            return Update(workline, true);
        }

        public List<MdWorkshop> GetMdWorkshops(int lineCode = 0)
        {
            if(lineCode == 0)
                return Context.Queryable<MdWorkshop>().ToList();
            else
               return  Context.Queryable<MdWorkshop>().Where(it=>it.Equals(lineCode)).ToList();         
        }
    }
}
